*This file replicates regression results for Investment and Employment outcomes - Falisification Test.
*The code used in this file is adapted from Baker, Bloom, and Davis (2016) to generate results related to environmental policy uncertainty.

cap cd "panel_regressions"
clear all
use data/all_firm_data_updated
set more off

so gvkey year quarter
merge 1:1 gvkey year quarter using data/tobin_q_data, keepus(tobin_q) 
ren tobin_q tobin
drop if _m==2  
drop _m
so gvkey year quarter
merge 1:1 gvkey year quarter using data/cashflow_data, keepus(cashflow)
tab _m
drop if _m==2
drop _m

merge m:1 year quarter using data/modern_epu_quarterly 

foreach var in E P U EP EU PU EPU {
	gen lm_`var'=log(m_`var')
}
drop if _merge==2
drop _merge
merge m:1 year quarter using data/categories_yq //
gen yq=year+(quarter-1)/4
gen lepu=log(m_EPU)
gen leu=log(m_EU)

********************Updating/Fixing VIX
cap drop _merge
ren vix oldvix
so yq
merge m:1 yq using data/vix.dta,update
replace lvix=log(vix)
drop _m
merge m:1 yq using data/vxo_quarterly.dta,update
drop _m
*replace vix with vxo
replace vix=vxo if vix==.&vxo~=.
replace lvix=log(vix)

***************Add in Steve's intensity measure
so gvkey
merg m:1 gvkey using data/gvkey
drop if _m==2
drop _m
so cusip
merge m:1 cusip using data/steve10k_cusip
winsor2 jp,replace 

drop if _m==2
drop _m
ren jp jp 

merge m:1 year quarter using data/04_Env_Policy_indices_qtr

************************************************************************************************************************
***Here we make all of the necessary interactions for each intensity measure
************************************************************************************************************************
tsset ticker_code period_code

foreach var of varlist h_firm_sic_intens  {
	foreach var2 of varlist m_* lm_* vix lepu lvix fed fed_fore leu {
		qui gen `var2'_X_`var' = `var2'*`var'
		qui gen l1`var2'_X_`var' = l1.`var2'_X_`var'
		qui gen dl1`var2'_X_`var' = d.l1`var2'_X_`var'
	}
}

foreach var of varlist firm_intens sic_intens firm_sic_intens belo_intens jp {
	foreach var2 of varlist lepu fed {
		qui gen `var2'_X_`var' = `var2'*`var'
		qui gen l1`var2'_X_`var' = l1.`var2'_X_`var'
		qui gen dl1`var2'_X_`var' = d.l1`var2'_X_`var'
	}
}
gen lagppent=l.ppent
gen l1lepu=l1.lepu
gen dl1lepu=d.l1lepu
gen l1fed=l1.fed
gen dl1fed=d.l1fed
gen SIC2=int(sic4/100)

* Sectoral policy indices
gen env_firms=1 if inlist(sic3, 104, 109, 122, 131, 138, 241, 242, 491, 492) | inlist(SIC2, 1, 2, 37)
replace env_firms=0 if env_firms==.
gen def_firms = inlist(sic3, 348, 381, 871) 
gen health_firms=(sic3>=800&sic3<=809)
gen fin_firms= (sic3>=600&sic3<=679) 

tsset ticker_code period_code

foreach var in EPU_NB EP_NB {
gen l`var'=log(`var')
}
foreach var in EPU_NB EP_NB {
foreach firms in env_firms def_firms health_firms fin_firms{
gen l`var'_X_`firms' = l`var'*`firms'
gen dl`var'_X_`firms'=d.l`var'_X_`firms'
gen dl`var'_X_`firms'_m=(dl`var'_X_`firms'==.)
replace dl`var'_X_`firms'=0 if dl`var'_X_`firms'_m==1
}
}

********************************************************************************
label var iqr_fed_gov_forecast "Fed Exp. IQR"

save data/itemp,replace
cap log close
cap log using invemp,replace t

****** Falsification Test (Table A1)
reghdfe invest_ppe_rate dlEPU_NB_X_env_firms dlEP_NB_X_env_firms dlEPU_NB_X_def_firms dlEP_NB_X_def_firms dlEPU_NB_X_health_firms  dlEP_NB_X_health_firms dlEPU_NB_X_fin_firms dlEP_NB_X_fin_firms  dl1lepu_X_h_firm_sic_intens dl1fed_X_h_firm_sic_intens,  ab(ticker_code period_code) vce(cl ticker)







***************************************************************************************************************************************************************************************************
*********************************************************************************************************************************************************
***************EMPLOYMENT
***Collapse to yearly data
clear all
use data/annual_firm_data 
gen period_code=year
tsset ticker_code period_code
gen one=1

foreach var of varlist one firm_intens sic_intens firm_sic_intens health_sic_intens health_firm_intens belo_intens h_firm_sic_intens {
	foreach var2 of varlist lepu leu lvix fed  fed_fore {
		qui gen `var2'_X_`var' = `var2'*`var'
		qui gen d`var2'_X_`var' = d.`var2'_X_`var'
	}
}

tsset 
gen demp = (emp - l.emp)/(0.5*emp+0.5*l.emp)
gen drev = (revtq - l.revtq)/(0.5*revtq+0.5*l.revtq)
replace drev = . if drev>=2 |drev<=-2

gen dlepu = d.lepu
gen dh_firm_sic_intens =d.h_firm_sic_intens
gen dfed = d.fed

gen SIC2=int(sic4/100)

merge m:1 year using data/04_Env_Policy_indices_annual
drop _m


* Sectoral policy indices
gen env_firms=1 if inlist(sic3, 104, 109, 122, 131, 138, 241, 242, 491, 492) | inlist(SIC2, 1, 2, 37)
replace env_firms=0 if env_firms==.
gen def_firms = inlist(sic3, 348, 381, 871) 
gen health_firms=(sic3>=800&sic3<=809)
gen fin_firms= (sic3>=600&sic3<=679) 

tsset ticker_code period_code

foreach var in EPU_NB EP_NB {
gen l`var'=log(`var')
}
foreach var in EPU_NB EP_NB {
foreach firms in env_firms def_firms health_firms fin_firms{
gen l`var'_X_`firms' = l`var'*`firms'
gen dl`var'_X_`firms'=d.l`var'_X_`firms'
gen dl`var'_X_`firms'_m=(dl`var'_X_`firms'==.)
replace dl`var'_X_`firms'=0 if dl`var'_X_`firms'_m==1
}
}

egen noj=count(demp),by(ticker_code)
keep if noj>1
save data/etemp,replace

****** Falsification Test (Table A1)
reghdfe demp dlEPU_NB_X_env_firms dlEP_NB_X_env_firms dlEPU_NB_X_def_firms dlEP_NB_X_def_firms dlEPU_NB_X_health_firms dlEP_NB_X_health_firms dlEPU_NB_X_fin_firms dlEP_NB_X_fin_firms  dlepu_X_h_firm_sic_intens dfed_X_h_firm_sic_intens, ab(ticker_code period_code) vce(cl ticker)
log close
